<template>
    <view class="pub_page pub_page_bg pub_index">
        <pub-tabbar :active="0" :adSrc="adSrc"></pub-tabbar>
        <z-paging ref="Zpaging" class="pub_page_scroll page_box" v-model="list" @query="getList"
            :refresher-enabled="false" :fixed="false" :empty-view-style="{padding:'0 0 140rpx 0'}"
            :empty-view-title-style="{color:'#fff'}">
            <div class="search_box" slot="top">
                <pub-image class="logo" :src="'../../static/aimg/img_22.png'" :mode="'aspectFit'"></pub-image>
                <pub-search class="search" placeholder="请输入您想要的宝贝" :btnType="2" @click="toSearch" />
            </div>
            <div class="banner_div" v-if="bannerList.length">
                <swiper class="swiper" circular :indicator-dots="true" :autoplay="true" :interval="5000"
                    previous-margin="30rpx" next-margin="30rpx" v-cloak:indicator-dots="false" @change="bannerChange">
                    <swiper-item v-for="(item, key) in bannerList" :key="key">
                        <view class="swiper_item" :class="{swiper_item_active:bannerIndex==key}"
                            :style="{ backgroundImage: 'url(' + item + ')' }"></view>
                    </swiper-item>
                </swiper>
            </div>
            <div class="notice_div" @click="noticeClick">
                <uni-notice-bar scrollable :speed="10" color="#333333" :text="noticeInfo.noticeDescribe" />
            </div>
            <div class="img_box" @click="$comFun.toast('敬请期待')">
                <pub-image class="img" :src="'../../static/aimg/img_63.png?a=1'" mode="widthFix" />
                <div class="ylzq" @click.stop="$comFun.openWin('/pages/gameDts/page/game')"></div>
            </div>
            <!-- <div class="tools_div">
                <div class="li" @click="$comFun.toast('敬请期待')">
                    <pub-image class="img" :src="'../../static/aimg/img_60.png'" />
                    <div class="txt">直营专区</div>
                </div>
                <div class="li" @click="$comFun.toast('敬请期待')">
                    <pub-image class="img" :src="'../../static/aimg/img_61.png'" />
                    <div class="txt">生活专区</div>
                </div>
                <div class="li" @click="$comFun.toast('敬请期待')">
                    <pub-image class="img" :src="'../../static/aimg/img_62.png'" />
                    <div class="txt">娱乐专区</div>
                </div>
            </div> -->
            <!-- <div class="goods_new_div">
                <div class="title">直营商品 福利多多 <div class="more"
                        @click="$comFun.openWin('/pages/goods/list', { orderType: 1 })">MORE</div>
                </div>
                <div class="left_img"></div>
                <div class="lis">
                    <div class="li" v-for="(li,key) in newList" :key="key"
                        @click="$comFun.openWin('/pages/index/good_info', { orderType: 1,id:li.id })">
                        <pub-image class="img" :src="li.coverImg" :mode="'aspectFit'"></pub-image>
                        <div class="text">
                            <div class="price">￥{{ li.sellPrice }}</div>
                            <div class="price_old">￥{{ li.price }}</div>
                        </div>
                    </div>
                </div>
            </div> -->
            <div class="goods_div" v-if="list.length">
                <div class="li" v-for="(li,key) in list" :key="key"
                    @click="$comFun.openWin('/pages/index/good_info', { orderType: 2,id:li.id })">
                    <div class="title">{{ li.goodsName }}</div>
                    <pub-image class="img" :src="li.coverImg" :mode="'aspectFit'"></pub-image>
                    <div class="cont">
                        <div class="text">
                            <div class="price">{{ li.sellPrice }}</div>
                            <div class="msg">
                                <div class="msg_1">￥</div>
                                <div class="msg_2">活动价</div>
                            </div>
                        </div>
                        <div class="btn">立即换购</div>
                    </div>
                </div>
            </div>
        </z-paging>
    </view>
</template>

<script>
export default {
    data() {
        return {
            bannerIndex: 0,
            bannerList: [],
            noticeInfo: {
                noticeDescribe: "",
                noticeId: "",
            },
            adSrc: '',

            searchVal: '',
            newList: [],
            list: []
        };
    },
    mounted() {
        this.getBanner();
        this.getNoticeInfo();
        // this.getNewList();
        this.getPopAd();
        this.getUserInfo();
        setTimeout(() => {
            this.$comFun.openLoading();
            this.$refs.Zpaging.reload()
        }, 400);
    },
    methods: {
        toSearch(data) {
            this.searchVal = data.searchVal
            this.$comFun.openLoading();
            this.$refs.Zpaging.reload()
        },
        async getPopAd() {
            const res = await this.$request.get('public/getAd', {
                key: 'open-ad'
            });
            // console.log(res);
            if (res.code == 200) {
                this.adSrc = res.result.length > 0 && res.result[0].image ? res.result[0].image : '';
            }
        },
        async getBanner() {
            const res = await this.$request.get('public/getAd', {
                key: 'index-ad'
            });
            // console.log(res);
            if (res.code == 200 && res.result.length > 0) {
                this.bannerList = res.result[0]?.image.split(",") || [];
            }
        },
        bannerChange(event) {
            this.bannerIndex = event.detail.current
        },
        async getNoticeInfo() {
            const res = await this.$request.get('public/getLastNotice', {});
            // console.log(res);
            if (res.code == 200 && res.result) {
                this.noticeInfo = res.result;
            }
        },
        noticeClick() {
            if (this.$store.state.isOnline == 1) {
                this.$comFun.openWin('/pages/other/document', { id: this.noticeInfo.noticeId });
            }
        },
        async getNewList() {
            let data = {
                pageNo: 1,
                pageSize: 3,
                categoryId: 1,
            };
            const res = await this.$request.get('goods/getGoodsPage', data);
            // console.log(res);
            if (res.code == 200) {
                this.newList = res.result.records
            }
        },
        async getList(pageNo, pageSize) {
            let data = {
                pageNo: pageNo,
                pageSize: pageSize,
                categoryId: 2,
                name: this.searchVal
            };
            const res = await this.$request.get('goods/getGoodsPage', data);
            this.$comFun.closeLoading();
            // console.log(res);
            if (res.code == 200) {
                this.$refs.Zpaging.complete(res.result.records);
            } else {
                this.$refs.Zpaging.complete(false);
            }
        },

        async getUserInfo() {
            const res = await this.$request.get('user/userInfo', {});
            // console.log(res);
            if (res.code == 200) {
                this.userInfo = res.result;
                if (!this.userInfo.province) {
                    // #ifdef APP-PLUS
                    this.getUserLocation();
                    // #endif
                    // #ifdef H5
                    this.locationInfo = {
                        latitude: 30.513494,
                        longitude: 104.060066,
                        address_txt: '四川省 成都市 双流区',
                        address: {
                            country: "中国",
                            province: "四川省",
                            city: "成都市",
                            district: "双流区",
                            street: "安公路",
                            streetNum: "3段-422",
                            cityCode: "75"
                        },
                    }
                    // this.saveUserLocation();
                    // #endif
                }
            }
        },
        async getUserLocation() {
            const that = this;
            // result 1=>已获取授权  0=>未获取授权-1=>被永久拒绝授权
            const result = await this.$permision.requestAndroidPermission('android.permission.ACCESS_FINE_LOCATION');
            console.log("result:", result);
            if (result == 1) {
                uni.getLocation({
                    type: 'gcj02',
                    geocode: true,
                    success: function (res) {
                        console.log(res);
                        that.locationInfo = {
                            latitude: res.latitude,
                            longitude: res.longitude,
                            address_txt: `${res.address.province} ${res.address.city} ${res.address.district}`,
                            address: res.address,
                        };
                        that.saveUserLocation();
                    }
                });
            } else {
                // this.dialogConfig = {
                //     title: '提示',//标题栏内容
                //     content: '请先获取定位信息再进入APP',
                //     btn_list: [{
                //         txt: '已设置',
                //         color: '#555',
                //         bg_1: '#e8e8e8',
                //         bg_2: '#eee',
                //     }, {
                //         txt: '去设置权限',
                //         color: '#fff',
                //         bg_1: '#31A5FF',
                //         bg_2: '#0974FF'
                //     }]
                // };
                // this.dialogShow = true;
            }
        },
        async saveUserLocation() {
            uni.setStorageSync('userLocationInfo', this.locationInfo);
            const res = await this.$request.post('user/bindPosition', {
                lat: this.locationInfo.latitude,
                lon: this.locationInfo.longitude,
            });
            console.log(res);
        },
    },
};
</script>

<style lang="scss" scoped>
@import "@/common/css/index.scss";
</style>
